package com.youlu.campus.service.wechat.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.CaInfo;
import com.youlu.campus.entity.SystemNickNameAndHeaderImage;
import com.youlu.campus.entity.VO.req.SystemNickNameAndHeaderImageQueryVO;
import com.youlu.campus.service.wechat.SystemNickNameAndHeaderImageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.regex.Pattern;


@Slf4j
@Service
public class SystemNickNameAndHeaderImageServiceImpl implements SystemNickNameAndHeaderImageService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public SystemNickNameAndHeaderImage getRandom() {
        Query query = new Query();
        Long count = mongoTemplate.count(query, SystemNickNameAndHeaderImage.class);
        Random rad = new Random();
        int radnum = rad.nextInt(count.intValue()) + 1;
        Query query1 = new Query().addCriteria(Criteria.where("no").is(radnum));
        return mongoTemplate.findOne(query1, SystemNickNameAndHeaderImage.class);
    }

    @Override
    public Page<SystemNickNameAndHeaderImage> list(SystemNickNameAndHeaderImageQueryVO req) {
        log.info(":>>> 系统头像列表:{}", JSON.toJSONString(req));
        if (Objects.isNull(req)) {
            log.error(":>>> 系统头像列表:参数错误");
            throw new BusinessException("系统头像列表:参数错误");
        }
        Query query = new Query();
        if (StringUtils.isNotBlank(req.getPlatform())) {
            query.addCriteria(Criteria.where("platform").is(req.getPlatform()));
        }
        if (StringUtils.isNotBlank(req.getType())) {
            query.addCriteria(Criteria.where("type").is(req.getType()));
        }
        if (StringUtils.isNotBlank(req.getGender())) {
            query.addCriteria(Criteria.where("gender").is(req.getGender()));
        }
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, SystemNickNameAndHeaderImage.class);
        PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<SystemNickNameAndHeaderImage> infos = mongoTemplate.find(query.with(pageRequest), SystemNickNameAndHeaderImage.class);
        return PageableExecutionUtils.getPage(infos, pageRequest, () -> count);
    }

    @Override
    public boolean create(SystemNickNameAndHeaderImage req) {
        Query query1 = new Query();
        Long count = mongoTemplate.count(query1, SystemNickNameAndHeaderImage.class);
        req.setId(null);
        req.setNo(count.intValue() + 1);
        req.setCreatedTime(new Date());
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean update(SystemNickNameAndHeaderImage req) {
        if (Objects.isNull(req) || StringUtils.isBlank(req.getId())) {
            log.error(":>>> 系统头像修改:参数错误");
            throw new BusinessException("系统头像修改:参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("id").is(req.getId()));
        boolean todo = false;
        Update update = new Update();
        if (!StringUtils.isBlank(req.getAvatarUrl())) {
            update.set("avatarUrl", req.getAvatarUrl());
            todo = true;
        }
        if (!StringUtils.isBlank(req.getNickName())) {
            update.set("nickName", req.getNickName());
            todo = true;
        }
        if (!StringUtils.isBlank(req.getPlatform())) {
            update.set("platform", req.getPlatform());
            todo = true;
        }
        if (!StringUtils.isBlank(req.getType())) {
            update.set("type", req.getType());
            todo = true;
        }
        if (!StringUtils.isBlank(req.getGender())) {
            update.set("gender", req.getGender());
            todo = true;
        }
        if (todo) {
            update.set("updatedTime", new Date());
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, SystemNickNameAndHeaderImage.class);

            return updateResult.getModifiedCount() > 0 ? true : false;
        }
        return true;
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("updatedTime", new Date());
        update.set("deleted", true);
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, SystemNickNameAndHeaderImage.class);

        return updateResult.getModifiedCount() > 0 ? true : false;
    }

    @Override
    public SystemNickNameAndHeaderImage findOne(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, SystemNickNameAndHeaderImage.class);
    }
}
